package chun.chun2.dao.impl;

import chun.chun2.dao.UserDao;
import chun.chun2.model.User;
import chun.chun2.util.DBUtil;

import java.sql.*;

public class UserDaoImpl implements UserDao {
    @Override
    public User findByUserNameAndPassword(String name, String pass) {
        {
            User user = null;
            String sql = "select * from user where username=? and password=?";
            int Id = 0;
            String email = null;

            DBUtil dbUtil = new DBUtil();
            try (Connection conn = dbUtil.getConnection();
                 PreparedStatement prep= conn.prepareStatement(sql);
                 ) {//执行查询


                prep.setString(1, name);
                prep.setString(2, pass);
                // 直接获取执行查询后的结果集
                try (ResultSet rs = prep.executeQuery();) {
                    while (rs.next()) {
                        user = new User();
                        user.setName(rs.getString("username"));
                        user.setId(rs.getInt("id"));
                        user.setUsername(rs.getString("username"));
                        user.setPassword(rs.getString("password"));
                        user.setEmail(rs.getString("email"));
                        user.setRole(rs.getString("role"));
                        user.setPhone(rs.getString("phone"));
                        user.setCreateTime(
                                rs.getTimestamp("create_time")
                                        .toLocalDateTime()
                        );
                        user.setStatus(rs.getInt("status"));
                    }
                    return user;
                }

            } catch (SQLException e) {
                throw new RuntimeException("连接或查询数据库异常",e);
            }


        }
    }

    @Override
    public boolean userSignUp() {return false;}



    @Override
    public User findByUserName() {return null; }


}
